/**
 * @Title MyExceptionResolver.java
 * @Package com.eppear.jpfm.framework.exception
 * @Description
 * @author JianFeng Zhang(张建锋)
 * @date 2016-12-27 上午12:16:37
 * @version 
 */
package com.eppear.jpfm.framework.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.eppear.jpfm.framework.common.ConfigConstants;
import com.eppear.jpfm.framework.common.ReturnMessage;
import com.eppear.jpfm.framework.constants.Constants;
import com.eppear.jpfm.framework.util.SysUtil;


/**
 * @ClassName MyExceptionResolver
 * @Description
 *         自定义异常处理解析器,用于把自定义的异常(运行时异常,业务异常)
 *         返回到客户端显示.
 * @author JianFeng Zhang(张建锋)
 * @date   2016-12-27 上午12:16:37
 *
 */
public class MyExceptionResolver implements HandlerExceptionResolver {
	
	private static Logger log =LoggerFactory.getLogger(MyExceptionResolver.class);
	private String errorPage="common/error";
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException
	 * (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, 
	 *  java.lang.Object, java.lang.Exception)
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest request, 
		HttpServletResponse response, Object handler,Exception ex) {
		
		Map<String,String> exMap = new HashMap<String,String>();
		ModelAndView mv=null;
		String errorMsg,errorCode;
		//StringPrintWriter strintPrintWriter = new StringPrintWriter();
		
				
        //ex.printStackTrace(strintPrintWriter);
        boolean isAjax = SysUtil.isAjax(request);
        
		//1.记录异常信息
		//log.error("系统运行异常,执行方法[{}],异常信息[{}]", handler, ex.getMessage());
		//log.error(strintPrintWriter.getString());
		
		//2.返回到前端界面
		/* 
		 * 同时如果是自定义异常时,取出相关的消息和代码返回.非自定义异常时,返回默认.
		 * 到前端界面,有两种方式,即AJAX和非AJAX时. 
		 * 
		 */
		errorMsg = ConfigConstants.SYS_INNER_ERROR;
		errorCode = "-1";
		if (ex instanceof MyException){
			MyException myEx = (MyException)ex;
			errorCode = myEx.getErrorCode() == null? null:myEx.getErrorCode().toString() ;
			errorMsg =  myEx.getErrorMessage();
			
			if (errorCode == null || "".equals(errorCode.trim()))
				errorCode="-1";
			if ( errorMsg == null || "".equals(errorMsg.trim()))
				errorMsg = ConfigConstants.SYS_INNER_ERROR;
									
		}
		if (isAjax) {
			ReturnMessage msg = new ReturnMessage(Integer.valueOf(errorCode),errorMsg);
			//可以通过如下语句设置跳转的页面给前端
			//msg.setParameterData(parameterData);
			SysUtil.renderJson(response, msg);
		}else {
			exMap.put(Constants.FIELD_ERROR_CODE, errorCode);
			exMap.put(Constants.FIELD_ERROR_MSG,errorMsg);
			//下一步在此设置可以跳转的地址
			mv = new ModelAndView(errorPage,exMap);
		}	
	
		return mv;
	}

}
